$(function(){
    'use strict';
    //只支持三层。一般三层就够了，更多层级会增加复杂度
     $("#table").delegate(".expand-icon", "click", function () {
         var id = $(this).attr("data-id");
         if ($(this).hasClass('child-open')) {
             $(this).removeClass('child-open');
             $(".tr-pid-"+id).hide();
             //所有的子项全部隐藏
             $(".tr-pid-"+id).each(function (){
                var id= $(this).find("i").attr("data-id");
                 $(".tr-pid-"+id).hide();
             });

             $(this).removeClass("icon-jiantou4");
             $(this).addClass("icon-down8");
             return ;
         }

         var that = $(this);
         $.ajax({
             url : "/auth/rule/list",
             type: 'POST',
             data: "rule_pid="+id,
             dataType : 'JSON',
             headers: {
                 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
             },
             success : function(data){
                 console.log(data.data.html);
                 if (data.data.html !='') {
                     that.parents("tr").after(data.data.html);
                     that.addClass("child-open");
                     that.removeClass("icon-down8");
                     that.addClass("icon-jiantou4");
                 }
             },
             error : function(e){
                 console.log(e);
             }
         });
     });
});